package com.jjh.users.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.service.BaseService;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.RegexUtil;
import com.jjh.users.pojo.ALog;
import com.jjh.users.service.IUsersDbService;
import com.jjh.users.service.IUsersOperService;

/**
 * 用户模块的线程池
 * 
 * @author MyJjh
 *
 */
@Component("usersCallableUtil")
@Scope("prototype")
public class UsersCallableUtil extends BaseService implements Callable<Object>
{
	/**
	 * 操作类型
	 */
	private String operType;
	@Autowired
	private IUsersDbService usersDbService;
	@Autowired
	private IUsersOperService usersOperService;
	@Autowired
	private RegexUtil regexUtil ;

	private Map<String,Object> paramsMap ;

	/**
	 * 用户模块所有和线程相关的代码进行放到这里
	 * 线程一启动会调用call方法,一个call方法如何处理不桶的操作请求
	 * Servelt每次访问会调用service方法,一个service方法如何处理不同的操作呢
	 */
	
	@Override
	public Object call() throws Exception
	{
		String threadName = Thread.currentThread().toString();
		ConstatFinalUtil.SYS_LOGGER.info(this.toString() + "====线程启动==" + threadName);
		if ("saveLog".equalsIgnoreCase(this.operType))
		{
			this.saveBatchLog();
		}else if ("statResBatchInsert".equalsIgnoreCase(operType))
		{
			this.statResBatchInsert();
		}
		ConstatFinalUtil.SYS_LOGGER.info("==线程终止:thread:{}==", threadName);
		return null;
	}

	public void setUsersOperService(IUsersOperService usersOperService)
	{
		this.usersOperService = usersOperService;
	}

	/**
	 * 批量插入统计结果表
	 */
	private void statResBatchInsert()
	{
		Date stDate = (Date) paramsMap.get("stDate") ; 
		Date edDate = (Date) paramsMap.get("edDate") ; 
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(stDate);
		/* 循环 */
		while(calendar.getTime().before(edDate))
		{
			Date dateTemp = calendar.getTime();
			/* 保存数据 */
			this.usersOperService.saveOneStatResService(dateTemp);
			calendar.add(Calendar.DATE, 1);
		}
	}

	/**
	 * 制作数据
	 */
	public void saveBatchLog()
	{
		String threadName = Thread.currentThread().toString();
		Random random = new Random();

		while (true)
		{
			try
			{
				/* 生成8-->29的随机数 */
				int adminsId = random.nextInt(29 - 7) + 8;
				if (adminsId == 30)
				{
					adminsId = 1;
				}

				ALog log = new ALog();
				log.setAdminsId(adminsId);
				/* 登录类型 */
				log.setLogType(random.nextInt(6));
				/* 内容 */
				log.setContent(threadName + "===" + this.regexUtil.proccedRandStr(30, InfoEnum.VAR_NUMCHAR));
				/* 随机生成ip */
				StringBuffer ipSb = new StringBuffer();
				for (int i = 0; i < 4; i++)
				{
					int ipTemp = random.nextInt(256);
					ipSb.append(ipTemp + ".");
				}
				/* 去掉最后一个. */
				ipSb.delete(ipSb.lastIndexOf("."), ipSb.length());
				log.setIp(ipSb.toString());
				/* 状态 */
				log.setStatus(Byte.valueOf(random.nextInt(2) + ""));
				/* 10年以内的时间随机 */
				Date now = new Date();
//				/* 单位秒 */
//				int year10 = 1 * 60 * 60 * 24 * 365 * 5;
//				/* 生成10年以内的随机数 */
//				int dateRand = random.nextInt(year10);
//				long currLong = System.currentTimeMillis();
//				/* 随机减去 */
//				currLong = currLong - Long.valueOf(dateRand) * 1000;
//				/* 设置时间;传入一个毫秒,变成哪年 */
//				now.setTime(currLong);

				log.setCreateTime(now);
				log.setUpdateTime(now);
				log.setPubTime(now);

				/* 保存一条记录 */
				JSONObject resultJSON = this.usersDbService.saveOneLogService(log);
				ConstatFinalUtil.SYS_LOGGER.info("保存成功:{}", resultJSON.toJSONString());
				/*
				 * 记录日志 adminId,logType,content,ip,status,createTime,updateTime,pubTime
				 */
//				ConstatFinalUtil.SYS_LOGIN.info("{},{},{},{},{},{},{},{}", log.getAdminsId(), log.getLogType(),
//						log.getContent(), log.getIp(), log.getStatus(),
//						this.dateUtil.dateTimeToStr(log.getCreateTime()),
//						this.dateUtil.dateTimeToStr(log.getUpdateTime()),
//						this.dateUtil.dateTimeToStr(log.getPubTime()));
				/* 休眠100毫秒随机 */
				Thread.sleep(random.nextInt(4) * 100);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}

}
